package com.atguigu.baku.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 时间工具类
 */
public class TimeUtils {

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 万能时间格式提取+转换，升级版！！！！强烈推荐！谨慎使用！
     * @param time
     * @return
     */
    public static String getTimeFromString2(String time) {
        Pattern p = Pattern.compile(".*?([0-9]{4})[-/年.]?([0-9]{1,2})[-/月.]?([0-9]{1,2})日?");
        Matcher m = p.matcher(time);
        if (m.find()) {
            String year = m.group(1);
            String month = m.group(2);
            String day = m.group(3);
            String time_r = year+"-"+month+"-"+day;
            return setCorrectTime(formatTime(time_r));

        }
        return "";
    }

    /**
     * 传入已经初步格式化之后的时间字符串,格式可能为yyyy-M-dd或是yyyy-MM-d或是yyyy-MM-dd,在这里统一转化为yyyy-MM-dd
     *
     * @param time
     * @return
     */
    private static String setCorrectTime(String time) {
        String[] temps = time.split("-");
        String year = temps[0];     //年份
        String month = temps[1].length() == 1 ? "0" + temps[1] : temps[1];   //月份
        String day = temps[2].length() == 1 ? "0" + temps[2] : temps[2];     //天数
        return year + "-" + month + "-" + day;
    }

    /**
     * 将初步得到的时间字符串中的非数字统统转换为-
     *
     * @param time
     * @return
     */
    private static String formatTime(String time) {
        Pattern p = Pattern.compile("[^0-9]");
        Matcher m = p.matcher(time);
        while (m.find()) {
            time = time.replace(m.group(), "-");
        }
        return time;
    }

    /**
     * 根据传入年限，返回最小值和最大值
     * @param dateEsta
     * @return
     */
    public static Map getDateEsta(String dateEsta) {
        Map map = new HashMap();
        Calendar c = Calendar.getInstance();
        String min = "";
        String max = "";
        try {
            switch (dateEsta) {
                case "1年以内":
                    c.add(Calendar.YEAR, -1);
                    min = sdf.format(new Date(c.getTimeInMillis()));
                    break;
                case "1-5年":
                    c.add(Calendar.YEAR, -1);
                    max = sdf.format(new Date(c.getTimeInMillis()));
                    c.add(Calendar.YEAR, -4);
                    min = sdf.format(new Date(c.getTimeInMillis()));
                    break;
                case "5-10年":
                    c.add(Calendar.YEAR, -5);
                    max = sdf.format(new Date(c.getTimeInMillis()));
                    c.add(Calendar.YEAR, -5);
                    min = sdf.format(new Date(c.getTimeInMillis()));
                    break;
                case "10-15年":
                    c.add(Calendar.YEAR, -10);
                    max = sdf.format(new Date(c.getTimeInMillis()));
                    c.add(Calendar.YEAR, -15);
                    min = sdf.format(new Date(c.getTimeInMillis()));
                    break;
                case "15年以上":
                    c.add(Calendar.YEAR, -15);
                    max = sdf.format(new Date(c.getTimeInMillis()));
                    break;
            }
            min = getTimeFromString2(min);
            max = getTimeFromString2(max);
            map.put("min", min);
            map.put("max", max);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("min", "");
            map.put("max", "");
        }
        return map;
    }

    public static void main(String[] args) {
        Map map = getDateEsta("1-5年");
        map.forEach((key, value) -> {
            System.out.println(key + "--" + value);
        });
    }

}
